_(){ _=$1;OFS=$IFS;IFS=.;set -- $_;IFS=$OFS;echo $1;};V=$1
[ -z "$V" ]&&echo "用法: $0 [版本]"&&exit 1
VV="5.4.2"
VVR="4.0.9"
AP="https://api.papermc.io/v2/projects/paper/versions"
UV="https://hangarcdn.papermc.io/plugins/ViaVersion"
M="4G";J=17;X="-XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1"
if ! type java >/dev/null 2>&1;then
apt update&&apt install -y openjdk-$J*||exit 1;fi
JV=$(java -version 2>&1|awk -F'"' '/version/{print $2}'|cut -d. -f1)
[ "$JV" -lt "$J" ]&&apt install -y --only-upgrade openjdk-$J*
for t in wget curl jq;do type $t >/dev/null 2>&1||apt install -y $t;done
U="${AP}/$V"
B=$(curl -s $U|jq '.builds[-1]')||{ echo "无效版本 $V";exit 1; }
D="${AP}/$V/builds/$B/downloads/paper-$V-$B.jar"
wget -q --show-progress -O paper.jar "$D"||{ echo "下载失败";exit 1; }
java -jar paper.jar nogui >/dev/null 2>&1
[ -f eula.txt ]&&sed -i 's/eula=false/eula=true/' eula.txt||echo "eula=true">eula.txt
[ -f server.properties ]&&sed -i 's/online-mode=true/online-mode=false/' server.properties
cat>start.sh<<'EOF'
A(){ T=$(grep MemTotal /proc/meminfo|awk '{print $2}');A=$(grep MemAvailable /proc/meminfo|awk '{print $2}')
M=$((A*75/100/1024));X=$((T*75/100/1024));M=$((M>X?X:M));M=$((M<1024?1024:M));M=$((M>32768?32768:M));echo $M;}
M=$(A);echo "内存: ${M}M";exec java -Xms${M}M -Xmx${M}M \
EOF
echo "$X -jar paper.jar nogui">>start.sh
mkdir plugins
cd plugins
wget ${UV}/ViaVersion/versions/${VV}/PAPER/ViaVersion-${VV}.jar
wget ${UV}/ViaBackwards/versions/${VV}/PAPER/ViaBackwards-${VV}.jar
wget ${UV}/ViaRewind/versions/${VVR}/PAPER/ViaRewind-${VVR}.jar
cd ..
chmod +x start.sh
echo "版本: PaperMC $V"
echo "端口: 25565"
echo "启动: ./start.sh"
echo "停止: 输入 stop"
./start.sh